/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.muni.fi.diplomka.datalayer.dao.BuildingInCityDao;
import cz.muni.fi.diplomka.datalayer.entity.BuildingInCityEntity;
import cz.fi.muni.diplomka.common.Coordinates;
import cz.fi.muni.diplomka.common.enumeration.BuildingType;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class BuildingInCityDaoImpl implements BuildingInCityDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public BuildingInCityDaoImpl() {
    }

    @Override
    public void addBuildingInCity(BuildingInCityEntity buildingInCityEntity) {
        if (buildingInCityEntity == null) {
            throw new IllegalArgumentException("BuildingInCityEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO building_in_city"
                    + "(city_id, x_coordinate, y_coordinate, name, level) VALUES"
                    + "(?,?,?,?,?)";
            ps = con.prepareStatement(insert);


            ps.setLong(1, buildingInCityEntity.getCityId());
            ps.setInt(2, buildingInCityEntity.getCoordinates().getX_coordinate());
            ps.setInt(3, buildingInCityEntity.getCoordinates().getY_coordinate());
            ps.setString(4, buildingInCityEntity.getName().toString());
            ps.setInt(5, buildingInCityEntity.getLevel());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(BuildingInCityDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editBuildingInCity(BuildingInCityEntity buildingInCityEntity) {
        if (buildingInCityEntity == null) {
            throw new IllegalArgumentException("BuildingInCityEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE building_in_city SET name=?, level=? WHERE city_id=? AND x_coordinate=? AND y_coordinate=?";

            ps = con.prepareStatement(update);

            ps.setString(1, buildingInCityEntity.getName().toString());
            ps.setInt(2, buildingInCityEntity.getLevel());
            ps.setLong(3, buildingInCityEntity.getCityId());
            ps.setInt(4, buildingInCityEntity.getCoordinates().getX_coordinate());
            ps.setInt(5, buildingInCityEntity.getCoordinates().getY_coordinate());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(BuildingInCityDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void deleteBuildingInCity(Long id, Coordinates coordinates) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        if (coordinates == null) {
            throw new IllegalArgumentException("Coordinates can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String delete = "DELETE FROM building_in_city WHERE city_id=?, x_coordinate=?, y_coordinate=?";

            ps = con.prepareStatement(delete);

            ps.setLong(1, id);
            ps.setInt(2, coordinates.getX_coordinate());
            ps.setInt(3, coordinates.getY_coordinate());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(BuildingInCityDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public BuildingInCityEntity getBuildingInCityAtPosition(Long cityId, Coordinates coordinates) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityID can not be null");
        }
        if (coordinates == null) {
            throw new IllegalArgumentException("Coordinates can not be null");
        }
        BuildingInCityEntity buildingInCityEntity = new BuildingInCityEntity();
        try {
            con = DbConnector.getDBConnection();
            String findBuildingInCity = "SELECT * FROM building_in_city WHERE city_id=? AND x_coordinate=? AND y_coordinate=?";

            ps = con.prepareStatement(findBuildingInCity);

            ps.setLong(1, cityId);
            ps.setInt(2, coordinates.getX_coordinate());
            ps.setInt(3, coordinates.getY_coordinate());

            rs = ps.executeQuery();
            while (rs.next()) {

                buildingInCityEntity.setCityId(rs.getLong("city_id"));
                Coordinates c = new Coordinates();
                c.setX_coordinate(rs.getInt("x_coordinate"));
                c.setY_coordinate(rs.getInt("y_coordinate"));
                buildingInCityEntity.setCoordinates(c);
                buildingInCityEntity.setName(BuildingType.valueOf(rs.getString("name")));
                buildingInCityEntity.setLevel(rs.getInt("level"));

            }

        } catch (SQLException ex) {
            Logger.getLogger(BuildingInCityDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return buildingInCityEntity;
    }

    @Override
    public List<BuildingInCityEntity> getAllBuildingInCity(Long cityId) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityID can not be null");
        }

        List<BuildingInCityEntity> getAllBuildingInCityList = new ArrayList<BuildingInCityEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllReceivedBuildingInCitysOfPlayer = "SELECT * FROM building_in_city WHERE city_id=?";

            ps = con.prepareStatement(getAllReceivedBuildingInCitysOfPlayer);
            ps.setLong(1, cityId);

            rs = ps.executeQuery();

            while (rs.next()) {
                BuildingInCityEntity buildingInCityEntity = new BuildingInCityEntity();
                buildingInCityEntity.setCityId(rs.getLong("city_id"));
                Coordinates c = new Coordinates();
                c.setX_coordinate(rs.getInt("x_coordinate"));
                c.setY_coordinate(rs.getInt("y_coordinate"));
                buildingInCityEntity.setCoordinates(c);
                buildingInCityEntity.setName(BuildingType.valueOf(rs.getString("name")));
                buildingInCityEntity.setLevel(rs.getInt("level"));

                getAllBuildingInCityList.add(buildingInCityEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(BuildingInCityDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return getAllBuildingInCityList;
    }

    @Override
    public List<BuildingInCityEntity> getAllBuildingInCityOfType(Long cityId, BuildingType buildingType) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityID can not be null");
        }

        List<BuildingInCityEntity> getAllBuildingInCityOfTypeList = new ArrayList<BuildingInCityEntity>();

        try {
            con = DbConnector.getDBConnection();
            String getAllBuildingInCityOfType = "SELECT * FROM building_in_city WHERE city_id=? AND name=?";

            ps = con.prepareStatement(getAllBuildingInCityOfType);
            ps.setLong(1, cityId);
            ps.setString(2, buildingType.toString());

            rs = ps.executeQuery();

            while (rs.next()) {
                BuildingInCityEntity buildingInCityEntity = new BuildingInCityEntity();
                buildingInCityEntity.setCityId(rs.getLong("city_id"));
                Coordinates c = new Coordinates();
                c.setX_coordinate(rs.getInt("x_coordinate"));
                c.setY_coordinate(rs.getInt("y_coordinate"));
                buildingInCityEntity.setCoordinates(c);
                buildingInCityEntity.setName(BuildingType.valueOf(rs.getString("name")));
                buildingInCityEntity.setLevel(rs.getInt("level"));

                getAllBuildingInCityOfTypeList.add(buildingInCityEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(BuildingInCityDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return getAllBuildingInCityOfTypeList;
    }
}
